package com.cskaoyan.service.impl;

import com.cskaoyan.bean.pojo.MallCoupon;
import com.cskaoyan.bean.pojo.MallCouponExample;
import com.cskaoyan.bean.pojo.MallUser;
import com.cskaoyan.mapper.MallCartMapper;
import com.cskaoyan.mapper.MallCouponMapper;
import com.cskaoyan.mapper.MallCouponUserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author: Chunlai
 * @description:
 * @time: 2021/7/14 10:48
 */
@Component
// 指定其执行顺序,值越小优先级越高
@Order(value = 1)
public class MyApplicationRunner implements ApplicationRunner {

    @Autowired
    MallCouponUserMapper mallCouponUserMapper;

    @Autowired
    MallCouponMapper mallCouponMapper;

    @Autowired
    MallCartMapper mallCartMapper;

    private static final Logger LOG = LoggerFactory.getLogger(MyApplicationRunner.class);
    /**
     * 工程启动结束后会立即执行的方法
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    Date date = new Date();
                    int hours = date.getHours();// 获取当前小时数(0-23)
                    int minutes = date.getMinutes();// 获取当前分钟数(0-59)
                    int seconds = date.getSeconds();// 获取当前秒数(0-59)
                    if (hours == 23 && minutes == 59 && seconds == 59) { //当前时间是 23：59：59 的时候
                        MallCouponExample mallCouponExample = new MallCouponExample();
                        MallCouponExample.Criteria criteria = mallCouponExample.createCriteria();
                        criteria.andStatusEqualTo(0);
                        // todo 后期有时间可以添加一下优惠券的个数
                        List<MallCoupon> mallCouponList = mallCouponMapper.selectByExample(mallCouponExample);
                        for (MallCoupon mallCoupon : mallCouponList) {
                            Date endTime = mallCoupon.getEndTime();
                            long endtime =  endTime.getTime();
                            long nowtime =  date.getTime();
                            long differ =  endtime - nowtime;
                            if (differ <= 0){ //过期的
                                mallCouponMapper.updatestatusById(mallCoupon.getId());
                            }
                        }
                    }
                }
            },0,1000);
    }




}